﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using U9API.Pub;
using System.Data;
using U9API.JsonModel;
using Newtonsoft.Json;
using System.Reflection;

namespace U9API.APIHelper
{

    public class ShipPlan
    {
        public Int64 ID { get; set; }

        public string DocumentType { get; set; }
        public string DocNo { get; set; }
        public string BusinessDate { get; set; }
        public string Planer { get; set; }
        
        public int SrcDocType { get; set; }
        
        public string Remark { get; set; }
        public int Status { get; set; }
        public List<ShipPlanLine> shipPlanLines { get; set; }
    }


    public class ShipPlanLine
    {
        public Int64 ID { get; set; }
        public string ItemInfo_ItemCode { get; set; }
        public Int64 ItemInfo_ItemID { get; set; }
        public string ConfigItem_itemcode { get; set; }
        public Int64 ConfigItem_ItemID { get; set; }
        public string OrderBy_Code { get; set; }
        public Int64 OrderBy_Customer { get; set; }
        public Int64 ShipToSite_CustomerSite { get; set; }
        public decimal OriginalItemQtyTU { get; set; }
        public string CasingNumber { get; set; }
        public decimal UsableQtyOfInv { get; set; }
        public string PlanDate { get; set; }
        public string  Remark { get; set; }
        public string  SrcKitLineNo { get; set; }
        public int  Status { get; set; }
             
        public string SrcDocNo { get; set; }
        public string ShipRule { get; set; }
        public string WH { get; set; }
        public Int64 SrcDoc { get; set; }
    }

    public class ShipPlanQueryHelper : BaseHelper<JsonModel.ApiQuery>
    { 
        public List<U9API.DBHelper.Model.Customer> Resdatas { get; set; }
        public string result;



        private ShipPlan DataRowToHModel(DataRow row)
        {
            ShipPlan model = new ShipPlan();
            Type examType = typeof(ShipPlan);
            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName, flag);
                        if (piInstance != null)
                        {
                            piInstance.SetValue(model, row[dc[i].ColumnName], null);
                        }
                    }
                }
            }

            return model;
        }

        private ShipPlanLine DataRowToBModel(DataRow row)
        {
            ShipPlanLine model = new ShipPlanLine();

            Type examType = typeof(ShipPlanLine);
            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName, flag);
                        if (piInstance != null)
                        {
                            piInstance.SetValue(model, row[dc[i].ColumnName], null);
                        }
                    }
                }
            }
            return model;
        }


        protected override void Create(string U9constr, int operation, List<JsonModel.ApiQuery> models )
        {
            //Pub.WriteLog.WriteLine(DateTime.Now.ToShortDateString() + ": CustomerHelper");
            if (models.Count == 0)
            {
                throw new Exception("查询条件不允许为空!");
            }
            JsonModel.ApiQuery model = models[0];
            StringBuilder sql = new StringBuilder();
            sql.Append(" select A.ID, A.DocNo, A2.Code as DocumentType, DocNo, Convert(varchar(10), BusinessDate,121) as  BusinessDate, A4.Name as Planer,  SrcDocType, Remark, Status from SM_ShipPlan A ");
            sql.Append(" LEFT JOIN SM_ShipPlan_Trl A1 ON A1.ID = A.ID ");
            sql.Append(" LEFT JOIN SM_ShipPlanDocType A2 ON A2.ID = A.DocumentType ");
            sql.Append(" LEFT JOIN CBO_Operators A3 ON A3.ID = A.Planer ");
            sql.Append(" LEFT JOIN CBO_Operators_Trl A4 ON A4.ID = A.Planer where 1=1 ");
           
            if (!string.IsNullOrEmpty(model.code))
            {
                sql.Append("and A.DocNo = '" + model.code + "'");
            }
            if (!string.IsNullOrEmpty(model.id))
            {
                sql.Append("and A.ID = '" + model.id + "'");
            }
            if (!string.IsNullOrEmpty(model.customer))
            {
                sql.Append("and exists (select 1 from SM_ShipPlanLine where SM_ShipPlanLine.ShipPlan = A.ID and SM_ShipPlanLine.OrderBy_Customer = '" + model.customer + "') ");
            }
            if (!string.IsNullOrEmpty(model.startdate))
            {
                sql.Append(" and (A.CreatedOn >='" + model.startdate + "' or A.ModifiedOn >='" + model.startdate +"')");
            }
            if (!string.IsNullOrEmpty(model.enddate))
            {
                sql.Append(" and (A.CreatedOn <='" + model.enddate + "' or A.ModifiedOn <='" + model.enddate + "')");
            }
            DataSet ds = DbHelperSQL.Query(sql.ToString(), U9constr);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                List<ShipPlan> mlist = new List<ShipPlan>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    mlist.Add(DataRowToHModel(ds.Tables[0].Rows[i]));
                }

                foreach (var item in mlist)
                {
                    StringBuilder sqlb = new StringBuilder();
                    sqlb.Append("  select A.SrcDoc, A.ID, A.ItemInfo_ItemCode, A.ItemInfo_ItemID, A.ConfigItem_itemcode, A.ConfigItem_ItemID, A.OrderBy_Code, A.OrderBy_Customer, A.ShipToSite_CustomerSite, A.PlanQtyTU as OriginalItemQtyTU, A4.DescFlexField_PubDescSeg10 as CasingNumber, A.UsableQtyOfInv,  Convert(varchar(10), A.PlanDate,121) as PlanDate, A1.Remark, A.SrcKitLineNo , A2.Name as WH, A.Status, A3.Name as ShipRule, A.SrcDocNo  ");
                    sqlb.Append(" from SM_ShipPlanLine A LEFT JOIN SM_ShipPlanLine_Trl A1 ON A1.ID = A.ID");
                    sqlb.Append(" LEFT JOIN CBO_Wh_Trl A2 ON A2.ID = A.WH ");
                    sqlb.Append(" LEFT JOIN CBO_ShipmentRule_Trl A3 ON A3.ID = A.ShipRule ");
                    sqlb.Append(" LEFT JOIN cbo_Itemmaster A4 on A4.ID = A.ItemInfo_ItemID ");
                    sqlb.Append(" WHERE A.ShipPlan = '" + item.ID + "' ");
                    //Pub.WriteLog.WriteLine(sql.ToString());

                    DataTable dtb = DbHelperSQL.QueryAsDT(sqlb.ToString(), U9constr);
                    if (dtb != null && dtb.Rows.Count > 0)
                    {
                        item.shipPlanLines = new List<ShipPlanLine>();
                        for (int i = 0; i < dtb.Rows.Count; i++)
                        {
                            item.shipPlanLines.Add(DataRowToBModel(dtb.Rows[i]));
                        }
                    }
                }
                result = mlist.ToJson();

            }
            else
            {
                throw new Exception("没有查到匹配的发货计划记录!");
                
            }
        }
         
    }
}
